package com.dandev.cmdemo.web;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.dandev.cmdemo.form.Contact;
import com.dandev.cmdemo.service.ContactService;

/**
 * Handles delete, edit and new contacts
 * 
 * @author Dan
 */
@Controller
@RequestMapping("/contact")
public class ContactController {
	
	@Autowired
    private ContactService contactService;
	
	/**
	 * Prepare edit page for viewing.  Loads parents if applicable and loads contact for edit if applicable
	 * @param contactId optional for edit
	 * @param contactParentId optional for new under a parent
	 * @param model
	 */
	@RequestMapping(method=RequestMethod.GET)
	public void getEdit(@RequestParam(value = "contactId", defaultValue = "0") Long contactId,
			@RequestParam(value = "contactParentId", defaultValue = "0") Long contactParentId, Model model) {
		Contact contact;
		
		// Edit
		if(contactId.intValue() != 0) {
			contact = contactService.getContact(contactId);
		//New With Parent
		} else if(contactParentId.intValue() != 0) {
			contact = new Contact();
			contact.setParent(contactService.getContact(contactParentId));
			//model.addAttribute("contactParent", contactService.getContact(contactParentId));
		// New - No Parent
		} else {
			contact = new Contact();
		}
		
		
	
		model.addAttribute("contactForm", contact);

	}
	
	/**
	 * Removes contact if it has no children.
	 * @param contactId of contact to be removed
	 * @return view name
	 */
	@RequestMapping(value="/delete", method=RequestMethod.GET)
	public String doDelete(@RequestParam(value = "contactId", defaultValue = "0") Long contactId) {
		if(contactId != 0) {
		
			contactService.removeContact(contactId);
		}
		
		return "redirect:/index";
	}
	
	/**
	 * Handles a new or edit post
	 * @param contactForm new or edit data
	 * @param result of validation
	 * @param status
	 * @param model
	 * @return view name
	 */
	@RequestMapping(method=RequestMethod.POST)
	public String postEdit(@ModelAttribute("contactForm") @Valid Contact contactForm, BindingResult result, SessionStatus status, Model model) {
		
		if (result.hasErrors()) {
			
			if(contactForm.getParentId().intValue() != 0) {
				contactForm.setParent(contactService.getContact(contactForm.getParentId()));
			}
			
			
			return "contact";
		}
		
		if(contactForm.getParentId() != null) {
			contactForm.setParent(contactService.getContact(contactForm.getParentId()));
		}
		
		contactService.addContact(contactForm);
		
		return "redirect:/index";
	}
	
}
